package com.mediacloud.app.newsmodule.fragment.baoliao.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mediacloud.app.assembly.util.FunKt;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.model.news.BaoLiaoMeta;
import com.mediacloud.app.newsmodule.fragment.baoliao.XPlayerVideoListPlayerUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yoake on 2018/8/29.
 */

public class BaoLiaoVideoListDetailAdapter extends RecyclerView.Adapter<BaoLiaoVideoListDetailAdapter.MyViewHolder> {

    private List<BaoLiaoMeta> list;
    private Context context;
    public XPlayerVideoListPlayerUtil videoListPlayerUtil;

    public BaoLiaoVideoListDetailAdapter(Context context, RecyclerView recyclerView) {
        this.context = context;
        this.list = new ArrayList<>();
        videoListPlayerUtil = new XPlayerVideoListPlayerUtil(context, recyclerView);
    }

    public void setList(List<BaoLiaoMeta> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void addList(List<BaoLiaoMeta> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void destroy() {
        if (videoListPlayerUtil != null) {
            videoListPlayerUtil.onDestroy();
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.baoliao_video_player_item1, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        final BaoLiaoMeta meta = list.get(position);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) holder.imageLayout.getLayoutParams();
        int width = context.getResources().getDisplayMetrics().widthPixels;
        lp.height = width * 9 / 16;
        holder.imageLayout.setLayoutParams(lp);
        if (position == 0) {
            videoListPlayerUtil.mengban = holder.mengban;
            holder.userLayout.setVisibility(View.VISIBLE);
            holder.recommendLayout.setVisibility(View.VISIBLE);
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.new_user_logo_login);
//            Glide.with(context).load(meta.getAvatar()).into(holder.avatar).onLoadFailed(drawable);
          //  GlideUtils.loadUrl(meta.getAvatar(), holder.avatar, null, drawable, false, true);
            FunKt.loadRound(holder.avatar,meta.getAvatar(),drawable,null);
            holder.nickName.setText(meta.getNickName());
        } else {
            holder.userLayout.setVisibility(View.GONE);
            holder.recommendLayout.setVisibility(View.GONE);
        }

        holder.contentContainer.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) holder.mengban.getLayoutParams();
                lp1.height = holder.contentContainer.getHeight();
                holder.mengban.setLayoutParams(lp1);
                if (position == 0) {
                    holder.mengban.setVisibility(View.GONE);
                } else {
                    holder.mengban.setVisibility(View.VISIBLE);
                }
                holder.contentContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        if (meta.getVideo() != null && meta.getVideo().size() > 0) {

            try {
                JSONObject jsonObject = new JSONObject(meta.getVideo().get(0));
                String poster = jsonObject.optString("poster");

                Glide.with(context).load(poster).into(holder.image);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        holder.newInfo.setText(meta.getContent());
        holder.comment.setText(meta.getCommentCount() + "");
        holder.like.setText(meta.getFavorCount() + "");
        holder.playTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (meta.getVideo() != null && meta.getVideo().size() > 0) {
                    try {
                        JSONObject jsonObject = new JSONObject(meta.getVideo().get(0));
                        holder.mengban.setVisibility(View.GONE);
                        videoListPlayerUtil.setPlayerContainer(holder.itemView, holder.imageLayout, holder.mengban);
                        videoListPlayerUtil.setVideoData(jsonObject);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout userLayout;
        ImageView avatar;
        TextView nickName;

        RelativeLayout imageLayout;
        ImageView image;
        ImageView playTag;

        TextView newInfo;

        LinearLayout likeLayout;
        TextView like;

        LinearLayout commentLayout;
        TextView comment;

        ImageView shareImage;

        LinearLayout recommendLayout;

        LinearLayout contentContainer;
        View mengban;

        public MyViewHolder(View itemView) {
            super(itemView);
            userLayout = itemView.findViewById(R.id.userLayout);
            avatar = itemView.findViewById(R.id.avatar);
            nickName = itemView.findViewById(R.id.nickName);
            imageLayout = itemView.findViewById(R.id.imageLayout);
            image = itemView.findViewById(R.id.image);
            playTag = itemView.findViewById(R.id.playTag);
            newInfo = itemView.findViewById(R.id.newInfo);
            likeLayout = itemView.findViewById(R.id.likeLayout);
            like = itemView.findViewById(R.id.like);
            commentLayout = itemView.findViewById(R.id.commentLayout);
            comment = itemView.findViewById(R.id.comment);
            shareImage = itemView.findViewById(R.id.shareImage);
            recommendLayout = itemView.findViewById(R.id.recommendLayout);
            contentContainer = itemView.findViewById(R.id.contentContainer);
            mengban = itemView.findViewById(R.id.mengban);
        }
    }
}